﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
 

namespace Southridge.View
{

  
    /// <summary>
    /// An implementation of <see cref="IValueConverter"/> and <see cref="IMultiValueConverter"/> that formats any bound data with a specified
    /// <see cref="FormatString"/>.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The <c>FormatConverter</c> class can be used to format bound data according to standard .NET formatting rules. It implements both the
    /// <see cref="IValueConverter"/> and <see cref="IMultiValueConverter"/> interfaces, thus enabling usage in both <c>Binding</c>s and
    /// <c>MultiBinding</c>s.
    /// </para>
    /// <para>
    /// Note that this converter does not support conversions back for its <see cref="IMultiValueConverter"/> implementation. Any attempt to
    /// convert multiple values back will return <see langword="null"/>.
    /// </para>
    /// </remarks>
    /// <example>
    /// The following example shows how a <c>FormatConverter</c> can be used to format a single value:
    /// <code lang="xml">
    /// <![CDATA[
    /// <Label Content="{Binding Name, Converter={FormatConverter {}Your name is '{0}'.}}"/>
    /// ]]>
    /// </code>
    /// </example>
    /// <example>
    /// The following example shows how a <c>FormatConverter</c> can be used to format multiple values:
    /// <code lang="xml">
    /// <![CDATA[
    /// <Label>
    ///		<Label.Content>
    ///			<MultiBinding Converter="{FormatConverter {}Your name is '{0}' and you were born on {1:dd/MM/yyyy}.}">
    ///				<Binding Path="Name"/>
    ///				<Binding Path="Dob"/>
    ///			</MultiBinding>
    ///		</Label.Content>
    /// </Label>
    /// ]]>
    /// </code>
    /// </example>
    [ContentProperty("FormatString")]
    [ValueConversion(typeof(object), typeof(string))]
    public class FormatConverter : DependencyObject, IValueConverter, IMultiValueConverter
    {
 

        /// <summary>
        /// Constructs a default instance of <c>FormatConverter</c>.
        /// </summary>
        public FormatConverter()
        {
        }

        
 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter != null)
            {
                string formatstring = parameter as string;
                return string.Format(culture, formatstring, value);
            }
            return value; 
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException(); 
        }
         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter != null)
            {
                string formatstring = parameter as string;
                return string.Format(culture, formatstring, values);
            }
            return values; 
        }

       
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException(); 
        }
    }
}